#!/bin/bash

# 定义颜色变量，好看嘛，方便嘛
RED="\e[41m\e[97m"    # 红底白字，用于错误/警告
GREEN="\e[42m\e[97m"  # 绿底白字，用于成功
BLUE="\e[44m\e[97m"   # 蓝底白字，用于信息
RESET="\e[0m"         # 重置颜色

install_java() {
    if ! command -v java >/dev/null 2>&1; then
        echo -e "${RED}警告: Java 未安装${RESET}"
        
        if command -v pkg >/dev/null 2>&1; then
            echo -e "${BLUE}正在尝试自动安装 Java (OpenJDK 17)...${RESET}"
            # 使用清华镜像源加速，不然可能会发生下载太慢
            sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/apt/termux-main stable main@' $PREFIX/etc/apt/sources.list
            pkg update -y && pkg upgrade -y
            if pkg install openjdk-17 -y; then
                echo -e "${GREEN}Java 安装成功!${RESET}"
            else
                echo -e "${RED}错误: Java 安装失败${RESET}"
                exit 1
            fi
        else
            echo -e "${RED}错误: 无法自动安装，请手动安装 Java${RESET}"
            exit 1
        fi
    else
        echo -e "${GREEN}Java 已安装 √${RESET}"
    fi
}

download_netease_mc_decrypter() {
    if [ ! -f "NeMcDecrypter.jar" ]; then
        echo -e "${BLUE}下载网易 Minecraft 解密工具...${RESET}"
        DOWNLOAD_URL="https://gitee.com/bedlock/lz/raw/master/NeMcDecrypter.jar"
        
        if curl --progress-bar -#Lo "NeMcDecrypter.jar" "${DOWNLOAD_URL}"; then
            chmod 777 "NeMcDecrypter.jar"
            echo -e "${GREEN}下载成功!${RESET}"
        else
            echo -e "${RED}错误: 下载失败${RESET}"
            echo "请检查网络连接或手动下载工具"
            exit 1
        fi
    else
        echo -e "${GREEN}解密工具已存在 √${RESET}"
    fi
}

main() {
    clear
    install_java
    download_netease_mc_decrypter
    echo -e "${BLUE}正在启动解密工具...${RESET}"
    java -jar "NeMcDecrypter.jar" || {
        echo -e "${RED}错误: 解密工具运行失败${RESET}"
        exit 1
    }
}

main